<?php
/**
 * Created by PhpStorm.
 * User: RenBin
 * Date: 2015/9/28
 * Time: 15:53
 */

namespace common\models\Service;


use common\models\DAO\UserCoupon;
use common\msg\UserMsg;

class DrawService {

    /*
     * 中奖处理
     */
    static function deal($param){
        $userID = $param['userID'];
        $integral = IntegralService::remainder($userID);
        $error = ['status'=>0, 'msg'=>UserMsg::$time_out];
        if($integral < 20){
            return ['status'=>0, 'msg'=>'账户积分不足!'];
        }
        $param = [
            ['id'=>4, 'chance'=>5],
            ['id'=>3, 'chance'=>15],
            ['id'=>2, 'chance'=>40],
            ['id'=>1, 'chance'=>40],
        ];
        $now = time();
        $_result = self::drawservice($param);
        $param['road'] = 1;
        $transaction = \Yii::$app->db->beginTransaction();
        $param['couponID'] =  0;
        $param['hongBaoID'] =  0;
        $param['lotteryID'] =  0;
        $param['desc'] = '抽奖';
        $param['userID'] = $userID;
        $param['integral'] = 20;
        $param['createTime'] = $now;
        $result = ['status'=>1, 'msg'=>'谢谢惠顾！'];
        $result['id'] = $_result;
        switch($_result){
            case 1:
                $param['desc'] = '抽奖中取10积分';
                $integral_data['recID'] = $userID;
                $integral_data['type'] = 9;
                $integral_data['integral'] = 10;
                $integral_data['des'] = '抽奖';
                $integral_result = IntegralService::addIntegral($integral_data);
                if(!$integral_result){
                    $transaction->rollBack();
                    return $error;
                }
                $result['msg'] = '恭喜你，抽中10积分！';
                break;
            case 2:
            case 3:
            case 4:
                $card = 20;
                $result['msg'] = '恭喜你，抽中0.2%年化劵！';
                if($_result == 3){
                    $card = 50;
                    $result['msg'] = '恭喜你，抽中0.5%年化劵！';
                }
                if($_result == 4){
                    $card = 100;
                    $result['msg'] = '恭喜你，抽中1%年化劵！';
                }
                $user_coupon = new UserCoupon();
                $user_coupon->userID = $userID;
                $user_coupon->createTime = $now;
                $user_coupon->card = $card;
                $lose_day = 31 + 1;
                $user_coupon->loseTime = date('Ymd', strtotime("+$lose_day day"));
                $user_coupon_result = $user_coupon->save();
                if(!$user_coupon_result){
                    $transaction->rollBack();
                    return $error;
                }
                break;
            default:
                break;
        }
        $integral_deduct = UserIntegral::deduct($param);
        if($integral_deduct){
            $transaction->commit();
            return $result;
        }else {
            $transaction->rollBack();
            return $error;
        }
    }
    /*
     * 奖品中奖逻辑
     */
    static function drawservice($totalPrizeInfo)
    {
        foreach ($totalPrizeInfo as $prize)
        {
            $prizeArray[$prize["id"]] = $prize["chance"];
        }
        $prizeSum = array_sum($prizeArray);
        foreach ($prizeArray as $key=>$sigChance)
        {
            $random = mt_rand(1,$prizeSum);
            if($random <= $sigChance)
            {
                $result = $key;
                break;
            }
            else
            {
                $prizeSum = $prizeSum - $sigChance;
            }
        }
        unset($prizeArray);
        return $result;
    }
}